package offline_navi;

import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.GraphHopper;
import com.graphhopper.PathWrapper;
import com.graphhopper.config.CHProfile;
import com.graphhopper.config.Profile;
import com.graphhopper.reader.osm.GraphHopperOSM;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.util.PointList;
import com.graphhopper.util.shapes.GHPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Hello world!
 */
public class offline_getroute {
    public static GraphHopper hopper;

    public static void offline_initileze() {
        // create one GraphHopper instance
        hopper = new GraphHopperOSM().forServer();
        hopper.setDataReaderFile("resources\\osm\\beijing-latest.osm.pbf");
        // where to store graphhopper files?
        hopper.setGraphHopperLocation("resources\\graph-cache");
        hopper.setEncodingManager(EncodingManager.create("car"));

        // see docs/core/profiles.md to learn more about profiles
        hopper.setProfiles(
                new Profile("car").setVehicle("car").setWeighting("fastest")
        );
        // this enables speed mode for the profile we call "car" here
        hopper.getCHPreparationHandler().setCHProfiles(
                new CHProfile("car")
        );

        // now this can take minutes if it imports or a few seconds for loading
        // of course this is dependent on the area you import
        hopper.importOrLoad();
    }

    public static PointList getRoute(List<GHPoint> points) {
        GHRequest ghRequest = new GHRequest(points).setProfile("car").setLocale(Locale.CHINA);
        GHResponse ghResponse = hopper.route(ghRequest);
        if (ghResponse.hasErrors()) {
            return null;
        } else {
            // use the best path, see the GHResponse class for more possibilities.
            PathWrapper path = ghResponse.getBest();
            PointList pointList = path.getPoints();
//
//            double distance = path.getDistance();
//            long timeInMs = path.getTime();
//
//            InstructionList il = path.getInstructions();
//// iterate over every turn instruction
//            for(Instruction instruction : il) {
//                instruction.getDistance();
//   ...
//            }
//
//// or get the json
//            List<Map<String, Object>> iList = il.createJson();
//
//// or get the result as gpx entries:
//            List<GPXEntry> list = il.createGPXList();

            return pointList;
        }
    }

    public static void main(String[] args) {
        offline_initileze();
        List<GHPoint> points = new ArrayList<>();
        GHPoint temp = new GHPoint(39.989643, 116.481028);
        points.add(temp);
        temp = new GHPoint(39.90816, 116.434446);
        points.add(temp);
        PointList pointList = getRoute(points);
        System.out.println(pointList.getSize());
        System.out.println(pointList);


    }
}
